﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Blog.Net.Business.BusinessObjects;
using Blog.Net.DataAccess.DataControls;

namespace Blog.Net
{
    /// <summary>
    /// Summary description for BlogNetRoleProvider
    /// </summary>
    public class BlogNetRoleProvider : RoleProvider
    {
        private string applicationName;

        public BlogNetRoleProvider() { }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            if (usernames != null && roleNames != null)
            {
                foreach (string userName in usernames)
                {
                    LoginBO member = LoginDataControl.Instance.GetByUserName(userName);

                    if (member != null)
                    {
                        foreach (string roleName in roleNames)
                        {
                            RoleBO role = RoleDataControl.Instance.GetByName(roleName);

                            if (role != null)
                                member.Roles.Add(role);
                        }

                        LoginDataControl.Instance.Save(member);
                    }
                }
            }
        }

        public override string ApplicationName
        {
            get
            {
                return applicationName;
            }
            set
            {
                applicationName = value;
            }
        }

        public override void CreateRole(string roleName)
        {
            RoleBO role = new RoleBO();
            role.Name = roleName;
            RoleDataControl.Instance.Save(role);
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            bool retVal = false;
            RoleBO role = RoleDataControl.Instance.GetByName(roleName);

            if (role != null)
                retVal = RoleDataControl.Instance.Delete(role);

            return retVal;
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override string[] GetAllRoles()
        {
            IList<RoleBO> roles = RoleDataControl.Instance.GetAll();
            string[] roleNames = new string[roles.Count];

            for (int i = 0; i < roles.Count; i++)
            {
                roleNames[i] = roles[i].Name;
            }

            return roleNames;
        }

        public override string[] GetRolesForUser(string username)
        {
            ArrayList roleNames = new ArrayList();
            LoginBO member = LoginDataControl.Instance.GetByUserName(username);

            if (member != null && member.Roles != null)
            {
                foreach (RoleBO role in member.Roles)
                {
                    roleNames.Add(role.Name);
                }
            }

            return (string[])roleNames.ToArray(typeof(string));
        }

        public string[] GetAvailableRolesForUser(string username)
        {
            ArrayList availRoleNames = new ArrayList();
            string[] allRoles = this.GetAllRoles();
            string[] userRoles = this.GetRolesForUser(username);

            foreach (string role in allRoles)
            {
                bool exists = false;
                foreach (string userRole in userRoles)
                {
                    if (userRole == role)
                        exists = true;
                }
                if (!exists) availRoleNames.Add(role);
            }

            return (string[])availRoleNames.ToArray(typeof(string));
        }

        public override string[] GetUsersInRole(string roleName)
        {
            string[] userNames = null;
            RoleBO role = RoleDataControl.Instance.GetByName(roleName);

            if (role != null && role.Logins != null)
            {
                userNames = new string[role.Logins.Count];

                for (int i = 0; i < role.Logins.Count; i++)
                {
                    userNames[i] = role.Logins[i].UserName;
                }
            }

            return userNames;
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            bool retVal = false;

            LoginBO member = LoginDataControl.Instance.GetByUserName(username);

            if (member != null && member.Roles != null)
            {
                foreach (RoleBO role in member.Roles)
                {
                    if (role.Name.Equals(roleName))
                    {
                        retVal = true;
                        break;
                    }
                }
            }

            return retVal;
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            if (usernames != null && roleNames != null)
            {
                foreach (string userName in usernames)
                {
                    LoginBO member = LoginDataControl.Instance.GetByUserName(userName);

                    if (member != null)
                    {
                        foreach (string roleName in roleNames)
                        {
                            RoleBO role = RoleDataControl.Instance.GetByName(roleName);

                            if (role != null && member.Roles.Contains(role))
                                member.Roles.Remove(role);
                        }

                        LoginDataControl.Instance.Save(member);
                    }
                }
            }
        }

        public override bool RoleExists(string roleName)
        {
            RoleBO role = RoleDataControl.Instance.GetByName(roleName);
            return role != null;
        }
    }
}